import { Component, OnInit, Input } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { FollowService } from './follow.component.service'
@Component({
    selector: 'app-follow-component',
    templateUrl: './follow.component.html',
    styleUrls: ['./follow.component.scss']
})
export class FollowComponent implements OnInit {

    // 传入 关联信息(订单号 售后单号 客户id 出库单号)
    @Input() id: string = ''
    // 传入 1、订单 2、售后 3、客户 4、出库
    @Input() type: string = ''
    /**
     * 跟进参数
     */
    followParam: any = {
        // 关联信息(订单号 售后单号 客户id 出库单号)
        relationInformation: "",
        // 1、订单 2、售后 3、客户 4、出库
        type: 3,
        // 文件列表
        enclosure: [],
        // 备注
        content: "",
        // 当前页
        page: 1,
        // 当天加载条数
        pageSize: 10
    }

    // 弹窗 跟进记录
    modalTable: any = {
        // 列表集合
        records: [],
        // 总条数
        total: 0
    }
    messageId: any = null
    constructor(
        private message: NzMessageService,
        private FollowService: FollowService,
    ) { }

    ngOnInit() {
        this.onFollowList()
        console.log('follow-nginit')
    }

    /**
     * 打开模态框(跟进) 初始化
     * @param id 
     */
    onFollowList() {
        this.followParam.relationInformation = this.id;
        this.followParam.type = this.type
        this.followParam.page = 1
        this.followParam.enclosure = []
        this.followParam.content = ''
        this.httpFollowList();
    }
    /**
       * 跟进查询
       */
    httpFollowList() {
        if (!this.id) {
            return
        }
        this.createBasicMessage();
        this.FollowService.followerLst(this.followParam).subscribe((res: any) => {
            this.removeBasicMessage();
            const { data, code } = res
            if (code != 0) {
                this.createMessage("error", res.message);
                return;
            }
            this.modalTable = data || { records: [] }
            this.modalTable.records?.forEach((element: any) => {
                element.enclosure = element.enclosure?.split(',')
            });
        }, err => {
            this.removeBasicMessage();
            this.createMessage("error", err.message);
        })
    }


    /**
   *  弹窗 页码改变
   * @param index 页码数
   */
    onModalPageIndexChange(index: Number) {
        this.followParam.page = index;
        this.httpFollowList()
    }
    /**
     * 弹窗 每页条数改变的回调
     * @param index 页码数
     */
    onModalPageSizeChange(index: Number) {
        this.followParam.pageSize = index;
        this.httpFollowList()
    }

    /**
         * 只保留文件名加后缀
         */
    formUrl(url: String) {
        return url ? url.substr(url.lastIndexOf('/') + 1) : ''
    }

    /**
     * 开启loading
     */
    createBasicMessage(): void {
        this.messageId = this.message.loading('正在请求...', { nzDuration: 0 }).messageId;
    }
    /**
     * 移除loading
     */
    removeBasicMessage() {
        this.message.remove(this.messageId);
        this.messageId = null;
    }
    /**
     * 全局展示操作反馈信息
     * @param type 其他提示类型 success:成功 error:失败 warning：警告
     */
    createMessage(type: any, str: any): void {
        this.message.create(type, str);
    }
}
